package pl.edu.pw.polygen.modeler.client.properties;


import java.util.List;

import pl.edu.pw.polygen.modeler.client.graph.IPropertyChangeListener;
import pl.edu.pw.polygen.modeler.client.object.IPolygenObject;
import pl.edu.pw.polygen.modeler.client.utils.Scale;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.vaadin.terminal.gwt.client.ApplicationConnection;
import com.vaadin.terminal.gwt.client.Paintable;
import com.vaadin.terminal.gwt.client.UIDL;

public class BoundaryProperties extends Composite implements Paintable {
	private VerticalPanel panel;
	private Label titleLabel;
	private TextBox boundary;
	private Scale scale;
	private List<IPolygenObject> polygenObjects;
	
	public BoundaryProperties(){
		super();
		this.initialize();
		scale = Scale.getInstance();
	}
	
	private void initialize(){
		panel = new VerticalPanel();
		panel.setBorderWidth(1);
		titleLabel = new Label();
		
		HorizontalPanel boundaryPanel = getBoundaryPanel();
		
		panel.add(titleLabel);
		panel.add(boundaryPanel);
		initWidget(panel);
	}
	
	private HorizontalPanel getBoundaryPanel(){
		HorizontalPanel boundaryPanel = new HorizontalPanel();
		boundary = new TextBox();
		boundary.addChangeHandler(new ChangeHandler() {
			
			@Override
			public void onChange(ChangeEvent event) {
				try{
					for(IPolygenObject o : polygenObjects){
						o.getModel().setBoundary(Integer.parseInt(boundary.getText()));
						((IPropertyChangeListener)o).notifyChanges();
					}
				}catch(NumberFormatException e){
					
				}
			}
		});
		
		boundary.setTitle("boundary");
		Label boundaryLabel = new Label("boundary");
		boundaryLabel.setWidth("51px");
		boundaryPanel.add(boundaryLabel);
		boundaryPanel.add(boundary);
		return boundaryPanel;
	}
	
	public void setTitle(String title){
		titleLabel.setText(title);
	}
	
	public void setModel(List<IPolygenObject> polygenObjects){
		this.polygenObjects = polygenObjects;
		this.boundary.setText("");
	}
	
	@Override
	public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
		// TODO Auto-generated method stub
		
	}
}
